Read and Write compressed data to a binary file using ASP.NET
Posted by: Suprotim Agarwal ,
on 9/13/2008,
in
Category ASP.NET
Abstract: The BinaryReader and BinaryWriter in System.IO namespace read and write primitive types in binary, to a stream. The System.IO.Compression.GZipStream provides methods and properties to compress and decompress the stream. In this article, we will learn how to use compression techniques to compress the stream and write it to a binary file. We will then use the BinaryReader and the GZipStream class to decompress and read the file to display it on the webpage.
Read and Write compressed data to a binary file using ASP.NET
The BinaryReader and BinaryWriter in System.IO namespace read and write primitive types in binary, to a stream. The System.IO.Compression.GZipStream provides methods and properties to compress and decompress the stream. In this article, we will learn how to use compression techniques to compress the stream and write it to a binary file. We will then use the BinaryReader and the GZipStream class to decompress and read the file to display it on the webpage.
We have different types of files on our computer. The Binary file is one such format. You cannot read these binary files by just opening them in the notepad. In this article, we will use the classes BinaryWriter and BinaryReader to add information to the file and to retrieve information from it. We will be using a FileStream constructor to open and modify the file if it already exists, or create a new file if it does not exist. You can then pass the FileStreamobject and use it to construct your BinaryReader and BinaryWriter. Let us see some code:
Read and Writing Binary Files Without Compression
Note: Use the namespaces System.IO
Writing Binary data
C#
string str = "Read and Write compressed data to a binary file”;
// Write to a binary file
FileStream fs = null;
BinaryWriter bw = null;
try
{
fs = new FileStream(@"C:\test.bin", FileMode.OpenOrCreate);
bw = new BinaryWriter(fs);
bw.Write(str);
}
catch (IOException ex)
{
}
finally
{
bw.Flush();
bw.Close();
fs.Close();
}
VB.NET
Dim str as string = "Read and Write compressed data to a binary file”
' Write to a binary file
Dim fs As FileStream = Nothing
Dim bw As BinaryWriter = Nothing
Try
fs = New FileStream("C:\test.bin", FileMode.OpenOrCreate)
bw = New BinaryWriter(fs)
bw.Write(str)
Catch ex As IOException
Finally
bw.Flush()
bw.Close()
fs.Close()
End Try
Reading Binary Data
C#
// Read from a binary file
FileStream fs1 = null;
BinaryReader br = null;
try
{
fs1 = new FileStream(@"C:\test.bin", FileMode.Open);
br = new BinaryReader(fs1);
string str1;
str1 = br.ReadString();
Response.Write(str1);
}
catch (IOException ex)
{
}
finally
{
br.Close();
fs1.Close();
}
VB.NET
' Read from a binary file
Dim fs1 As FileStream = Nothing
Dim br As BinaryReader = Nothing
Try
fs1 = New FileStream("C:\test.bin", FileMode.Open)
br = New BinaryReader(fs1)
Dim str1 As String
str1 = br.ReadString()
Response.Write(str1)
Catch ex As IOException
Finally
br.Close()
fs1.Close()
End Try
Read and Writing Binary Files Using Compression
Note: Use the namespaces System.IO.Compression. Here instead of passing the file stream object directly to the BinaryWriter, we are passing the stream to GZipStream to compress it. The compressed stream is then passed to BinaryWriter. Similarly while reading the file, we pass the stream to GZipStream to decompress and then use BinaryReader to read the file and display its contents.
Writing Binary data
C#
// Compress and write to a binary file
FileStream fs = null;
BinaryWriter bw = null;
try
{
fs = new FileStream(@"C:\test1.bin", FileMode.OpenOrCreate);
GZipStream cmp = new GZipStream(fs, CompressionMode.Compress);
bw = new BinaryWriter(cmp);
bw.Write(str);
}
catch (IOException ex)
{
}
finally
{
bw.Flush();
bw.Close();
fs.Close();
}
VB.NET
' Compress and write to a binary file
Dim fs As FileStream = Nothing
Dim bw As BinaryWriter = Nothing
Try
fs = New FileStream("C:\test1.bin", FileMode.OpenOrCreate)
Dim cmp As GZipStream = New GZipStream(fs, CompressionMode.Compress)
bw = New BinaryWriter(cmp)
bw.Write(str)
Catch ex As IOException
Finally
bw.Flush()
bw.Close()
fs.Close()
End Try
Reading Binary Data
C#
// Decompress and read from a binary file
FileStream fs1 = null;
BinaryReader br = null;
try
{
fs1 = new FileStream(@"C:\test1.bin", FileMode.Open);
GZipStream dcmp = new GZipStream(fs1,
CompressionMode.Decompress);
br = new BinaryReader(dcmp);
string str1;
str1 = br.ReadString();
Response.Write(str1);
}
catch (IOException ex)
{
}
finally
{
br.Close();
fs1.Close();
}
VB.NET
' Decompress and read from a binary file
Dim fs1 As FileStream = Nothing
Dim br As BinaryReader = Nothing
Try
fs1 = New FileStream("C:\test1.bin", FileMode.Open)
Dim dcmp As GZipStream = New GZipStream(fs1, CompressionMode.Decompress)
br = New BinaryReader(dcmp)
Dim str1 As String
str1 = br.ReadString()
Response.Write(str1)
Catch ex As IOException
Finally
br.Close()
fs1.Close()
End Try
In this article, we saw how to use compression techniques to compress the stream and write it to a binary file. We then used the BinaryReader and the GZipStream class to decompress the stream and read the file to display it on the webpage. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn